<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JWTEncoder Class Reference</title>
    <link rel="stylesheet" type="text/css" href="../css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="../css/highlight.css" />
    <meta charset="utf-8">
    <script src="../js/jquery.min.js" defer></script>
    <script src="../js/jazzy.js" defer></script>
    
    <script src="../js/lunr.min.js" defer></script>
    <script src="../js/typeahead.jquery.js" defer></script>
    <script src="../js/jazzy.search.js" defer></script>
  </head>
  <body>

    <a name="//apple_ref/swift/Class/JWTEncoder" class="dashAnchor"></a>

    <a title="JWTEncoder Class Reference"></a>

    <header class="header">
      <p class="header-col header-col--primary">
        <a class="header-link" href="../index.html">
          SwiftJWT 3.6.200 Docs
        </a>
         (100% documented)
      </p>
    
      <p class="header-col--secondary">
        <form role="search" action="../search.json">
          <input type="text" placeholder="Search documentation" data-typeahead>
        </form>
      </p>
    
        <p class="header-col header-col--secondary">
          <a class="header-link" href="https://github.com/Kitura/Swift-JWT/">
            <img class="header-icon" src="../img/gh.png"/>
            View on GitHub
          </a>
        </p>
    
    </header>

    <p class="breadcrumbs">
      <a class="breadcrumb" href="../index.html">SwiftJWT Reference</a>
      <img class="carat" src="../img/carat.png" />
      JWTEncoder Class Reference
    </p>

    <div class="content-wrapper">
      <nav class="navigation">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Classes.html">Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/ClaimsMicroProfile.html">ClaimsMicroProfile</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/ClaimsOpenID.html">ClaimsOpenID</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/ClaimsStandardJWT.html">ClaimsStandardJWT</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/JWTDecoder.html">JWTDecoder</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/JWTEncoder.html">JWTEncoder</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Protocols.html">Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Protocols/Claims.html">Claims</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Structs.html">Structures</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/AddressClaim.html">AddressClaim</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/Header.html">Header</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/JWT.html">JWT</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/JWTError.html">JWTError</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/JWTSigner.html">JWTSigner</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/JWTVerifier.html">JWTVerifier</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/ValidateClaimsResult.html">ValidateClaimsResult</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">

        <section class="section">
          <div class="section-content top-matter">
            <h1>JWTEncoder</h1>
              <div class="declaration">
                <div class="language">
                  
                  <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">class</span> <span class="kt">JWTEncoder</span> <span class="p">:</span> <span class="kt">BodyEncoder</span></code></pre>

                </div>
              </div>
            <p>A thread safe encoder that signs the JWT header and claims using the provided algorithm and encodes a <code><a href="../Structs/JWT.html">JWT</a></code> instance as either Data or a JWT String.</p>
<h3 id='usage-example' class='heading'>Usage Example:</h3>
<pre class="highlight swift"><code><span class="kd">struct</span> <span class="kt">MyClaims</span><span class="p">:</span> <span class="kt">Claims</span> <span class="p">{</span>
   <span class="k">var</span> <span class="nv">name</span><span class="p">:</span> <span class="kt">String</span>
<span class="p">}</span>
<span class="k">var</span> <span class="nv">jwt</span> <span class="o">=</span> <span class="kt">JWT</span><span class="p">(</span><span class="nv">claims</span><span class="p">:</span> <span class="kt">MyClaims</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span> <span class="s">"John Doe"</span><span class="p">))</span>
<span class="k">let</span> <span class="nv">privateKey</span> <span class="o">=</span> <span class="s">"&lt;PrivateKey&gt;"</span><span class="o">.</span><span class="nf">data</span><span class="p">(</span><span class="nv">using</span><span class="p">:</span> <span class="o">.</span><span class="n">utf8</span><span class="p">)</span><span class="o">!</span>
<span class="k">let</span> <span class="nv">rsaJWTEncoder</span> <span class="o">=</span> <span class="kt">JWTEncoder</span><span class="p">(</span><span class="nv">jwtSigner</span><span class="p">:</span> <span class="kt">JWTSigner</span><span class="o">.</span><span class="nf">rs256</span><span class="p">(</span><span class="nv">privateKey</span><span class="p">:</span> <span class="n">privateKey</span><span class="p">))</span>
<span class="k">do</span> <span class="p">{</span>
   <span class="k">let</span> <span class="nv">jwtString</span> <span class="o">=</span> <span class="k">try</span> <span class="n">rsaJWTEncoder</span><span class="o">.</span><span class="nf">encodeToString</span><span class="p">(</span><span class="n">jwt</span><span class="p">)</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">{</span>
   <span class="nf">print</span><span class="p">(</span><span class="s">"Failed to encode JWT: </span><span class="se">\(</span><span class="n">error</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>

          </div>
        </section>

        <section class="section">
          <div class="section-content">
            <div class="task-group">
              <div class="task-name-container">
                <a name="/Initializers"></a>
                <a name="//apple_ref/swift/Section/Initializers" class="dashAnchor"></a>
                <div class="section-name-container">
                  <a class="section-name-link" href="#/Initializers"></a>
                  <h3 class="section-name"><p>Initializers</p>
</h3>
                </div>
              </div>
              <ul class="item-container">
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT10JWTEncoderC9jwtSignerAcA9JWTSignerV_tcfc"></a>
                    <a name="//apple_ref/swift/Method/init(jwtSigner:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT10JWTEncoderC9jwtSignerAcA9JWTSignerV_tcfc">init(jwtSigner:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Initialize a <code>JWTEncoder</code> instance with a single <code><a href="../Structs/JWTSigner.html">JWTSigner</a></code>.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">jwtSigner</span><span class="p">:</span> <span class="kt"><a href="../Structs/JWTSigner.html">JWTSigner</a></span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>jwtSigner</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The <code><a href="../Structs/JWTSigner.html">JWTSigner</a></code> that will be used to sign the JWT.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>A new instance of <code>JWTEncoder</code>.</p>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT10JWTEncoderC13keyIDToSignerAcA9JWTSignerVSgSSc_tcfc"></a>
                    <a name="//apple_ref/swift/Method/init(keyIDToSigner:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT10JWTEncoderC13keyIDToSignerAcA9JWTSignerVSgSSc_tcfc">init(keyIDToSigner:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Initialize a <code>JWTEncoder</code> instance with a function to generate the <code><a href="../Structs/JWTSigner.html">JWTSigner</a></code> from the JWT <code>kid</code> header.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">keyIDToSigner</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt"><a href="../Structs/JWTSigner.html">JWTSigner</a></span><span class="p">?)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>keyIDToSigner</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The function to generate the <code><a href="../Structs/JWTSigner.html">JWTSigner</a></code> from the JWT <code>kid</code> header.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>A new instance of <code>JWTEncoder</code>.</p>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
            <div class="task-group">
              <div class="task-name-container">
                <a name="/Encode"></a>
                <a name="//apple_ref/swift/Section/Encode" class="dashAnchor"></a>
                <div class="section-name-container">
                  <a class="section-name-link" href="#/Encode"></a>
                  <h3 class="section-name"><p>Encode</p>
</h3>
                </div>
              </div>
              <ul class="item-container">
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT10JWTEncoderC6encodey10Foundation4DataVxKSERzlF"></a>
                    <a name="//apple_ref/swift/Method/encode(_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT10JWTEncoderC6encodey10Foundation4DataVxKSERzlF">encode(_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Encode a <code><a href="../Structs/JWT.html">JWT</a></code> instance into a UTF8 encoded JWT String.</p>
<div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <code><a href="../Structs/JWTError.html#/s:8SwiftJWT8JWTErrorV15invalidUTF8DataACvpZ">JWTError.invalidUTF8Data</a></code> if the provided Data can&rsquo;t be decoded to a String.

</div><div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <code><a href="../Structs/JWTError.html#/s:8SwiftJWT8JWTErrorV12invalidKeyIDACvpZ">JWTError.invalidKeyID</a></code> if the KeyID <code>kid</code> header fails to generate a jwtSigner.

</div><div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <code>EncodingError</code> if the encoder fails to encode the object as Data.

</div>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="n">encode</span><span class="o">&lt;</span><span class="kt">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span> <span class="nv">value</span><span class="p">:</span> <span class="kt">T</span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="kt">Data</span> <span class="k">where</span> <span class="kt">T</span> <span class="p">:</span> <span class="kt">Encodable</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>value</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The JWT instance to be encoded as Data.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>The UTF8 encoded JWT String.</p>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT10JWTEncoderC14encodeToStringySSxKSERzlF"></a>
                    <a name="//apple_ref/swift/Method/encodeToString(_:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT10JWTEncoderC14encodeToStringySSxKSERzlF">encodeToString(_:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Encode a <code><a href="../Structs/JWT.html">JWT</a></code> instance as a JWT String.</p>
<div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <code><a href="../Structs/JWTError.html#/s:8SwiftJWT8JWTErrorV12invalidKeyIDACvpZ">JWTError.invalidKeyID</a></code> if the KeyID <code>kid</code> header fails to generate a jwtSigner.

</div><div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <code>EncodingError</code> if the encoder fails to encode the object as Data.

</div>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="n">encodeToString</span><span class="o">&lt;</span><span class="kt">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span> <span class="nv">value</span><span class="p">:</span> <span class="kt">T</span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="kt">String</span> <span class="k">where</span> <span class="kt">T</span> <span class="p">:</span> <span class="kt">Encodable</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>value</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The JWT instance to be encoded as a JWT String.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>A JWT String.</p>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT10JWTEncoderC22base64urlEncodedString4dataSS10Foundation4DataV_tFZ"></a>
                    <a name="//apple_ref/swift/Method/base64urlEncodedString(data:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT10JWTEncoderC22base64urlEncodedString4dataSS10Foundation4DataV_tFZ">base64urlEncodedString(data:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Returns a <code>String</code> representation of this data, encoded in base64url format
as defined in RFC4648 (<a href="https://tools.ietf.org/html/rfc4648">https://tools.ietf.org/html/rfc4648</a>).</p>

<p>This is the appropriate format for encoding the header and claims of a JWT.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">static</span> <span class="kd">func</span> <span class="nf">base64urlEncodedString</span><span class="p">(</span><span class="nv">data</span><span class="p">:</span> <span class="kt">Data</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">String</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </section>

      </article>
    </div>
    <section class="footer">
      <p>&copy; 2020 <a class="link" href="https://github.com/Kitura/Swift-JWT" target="_blank" rel="external">IBM &amp; Kitura Project Contributors</a>. All rights reserved. (Last updated: 2020-11-25)</p>
      <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
    </section>
  </body>
</div>
</html>
